import Vue from 'vue';
import app from './app.vue';
import MintUi from 'mint-ui';
Vue.use(MintUi);
import 'mint-ui/lib/style.css';
import 'bootstrap/dist/css/bootstrap.css'; 
import './lib/mui/css/mui.min.css';
import './css/icons-extra.css';
import VueRouter from 'vue-router';
Vue.use(VueRouter);
import router from './router.js';
import VueResource from 'vue-resource';
Vue.use(VueResource);
Vue.http.options.root = '/src/mydata/';
import moment from 'moment'; 
// import { Lazyload } from 'mint-ui'; 
// Vue.use(Lazyload);
import VuePreview from 'vue-preview';
Vue.use(VuePreview);
import Vuex from 'vuex';
Vue.use(Vuex);

Vue.filter('dataFormat',function(dataStr,pattern='YYYY-MM-DD HH:mm:ss'){
    return moment(dataStr).format(pattern);
})

var carStore = JSON.parse(localStorage.getItem("car" || "[]"));
var car = [];
if(carStore!=null){
    car = carStore;
}
var store = new Vuex.Store({
    state:{
        car:car
    },
    mutations:{
        optCar:function(state,carObj){
            var flag = false;
            state.car.some(item=>{
                if(item.id===carObj.id){
                    item.count += parseInt(carObj.count);
                    flag = true;
                    return true;
                }
            });
            if(!flag){
                state.car.push(carObj);
            }
            localStorage.setItem("car",JSON.stringify(state.car));
        },
        updateCar:function(state,carObj){ 
            state.car.some(item=>{
                if(item.id==carObj.id){ 
                    item.count = parseInt(carObj.count);
                    return true;
                }
            });
            localStorage.setItem("car",JSON.stringify(state.car));
        },
        remove:function(state,id){ 
            state.car.some((item,i)=>{
                if(item.id==id){ 
                    state.car.splice(i,1);
                    return true;
                }
            });
            localStorage.setItem("car",JSON.stringify(state.car));
        },
        updateSelected:function(state,carStatus){
            state.car.some(item=>{
                if(item.id==carStatus.id){
                    item.selected = carStatus.selected;
                    return true;
                }
            })
            localStorage.setItem("car",JSON.stringify(state.car));
        }
    },
    getters:{
        getCarInfo:function(state){
            console.log(state.car);
            var c = 0;
            state.car.forEach(item=>{
                c += item.count;
            });
            return c;
        },
        getGoodsCount:function(state){
            var o = {};
            state.car.forEach(item=>{
                o[item.id] = item.count;
            });
            return o;
        },
        getSelected:function(state,id){
            var o = {};
            state.car.forEach(item=>{
                o[item.id] = item.selected;
            });
            return o;
        },
        getCountAndAmount(state){
            var o = {
                count:0,
                amount:0
            };
            state.car.forEach(item=>{
                if(item.selected){
                    o.count += item.count;
                    o.amount += item.sell_price * item.count;
                }
            })
            return o;
        }
    }
});

var vm = new Vue({
    el:'#app',
    render:c=>c(app),
    router,
    store,
    methods:{

    }
});